package com.simon.jkxj

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.simon.jkxj.databinding.ActivityLoginBinding
import com.simon.jkxj.MainActivity
import com.simon.jkxj.RegisterActivity
import com.simon.jkxj.model.AppDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class LoginActivity : AppCompatActivity() {
    private lateinit var binding: ActivityLoginBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setupClickListeners()
    }

    private fun setupClickListeners() {
        binding.btnLogin.setOnClickListener {
            val phone = binding.etPhone.text.toString()
            val password = binding.etPassword.text.toString()

            if (validateInput(phone, password)) {
                lifecycleScope.launch {
                    val userDao = AppDatabase.getDatabase(this@LoginActivity).userDao()
                    val user = withContext(Dispatchers.IO) { userDao.login(phone, password) }
                    if (user != null) {
                        withContext(Dispatchers.Main) {
                            if (user.gender.isNullOrEmpty() || user.gender == "保密") {
                                Toast.makeText(this@LoginActivity, "请完善个人信息", Toast.LENGTH_SHORT).show()
                                val intent = Intent(this@LoginActivity, CompleteProfileActivity::class.java)
                                intent.putExtra("userId", user.id)
                                startActivity(intent)
                            } else {
                                Toast.makeText(this@LoginActivity, "登录成功", Toast.LENGTH_SHORT).show()
                                startActivity(Intent(this@LoginActivity, MainActivity::class.java))
                            }
                            finish()
                        }
                    } else {
                        withContext(Dispatchers.Main) {
                            Toast.makeText(this@LoginActivity, "手机号或密码错误", Toast.LENGTH_SHORT).show()
                        }
                    }
                }
            }
        }
        binding.tvRegister.setOnClickListener {
            startActivity(Intent(this, RegisterActivity::class.java))
        }
    }

    private fun validateInput(phone: String, password: String): Boolean {
        if (phone.isEmpty()) {
            binding.tilPhone.error = "请输入手机号"
            return false
        }
        if (phone.length != 11) {
            binding.tilPhone.error = "请输入正确的手机号"
            return false
        }
        if (password.isEmpty()) {
            binding.tilPassword.error = "请输入密码"
            return false
        }
        if (password.length < 6) {
            binding.tilPassword.error = "密码长度不能少于6位"
            return false
        }
        return true
    }
} 